package com.wwwarehouse.common.custom_widget.progressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wwwarehouse.common.R;


/**
 * @author : mundane
 * @time : 2017/6/23 11:49
 * @description :
 * @file : StepView.java
 */

public class StepView extends View {

	private int mTotalStep = 4;
	private int mCurrentStep = 2;
	private Paint mCompletePaint;
	private Paint mUncompletePaint;
	private Paint mTransparentPaint;
	private int mCurrentPositionCircleColor = getResources().getColor(R.color.common_color_c11_ff1f1f);
	private Paint mTextPaint;
	private int mCompleteColor = getResources().getColor(R.color.common_color_c1_337cff);
		private int mUncompleteColor = getResources().getColor(R.color.common_color_c8_d5d7dc);
	private int mTextColor = getResources().getColor(android.R.color.white);
	private int mTransparentColor = getResources().getColor(android.R.color.transparent);
	private float mUncompleteRadius = dp2px(8);
	private float mCompleteRadius = dp2px(13);
	private int mLineHeight = dp2px(2);
	private float mPaddingLeft = dp2px(30);
	private float mPaddingTop = dp2px(30);
	private int mWidth;
	private int mHeight;
	private float mLineWidth;
	private final int HORIZONTAL = 0;
	private final int VERTICAL = 1;
	private int mOritation;
	private float mSectionHeight;
	private float mBorder = dp2px(1);
	private final int STYLE_COMMON = 0;
	private final int STYLE_RED = 1;
	private final int STYLE_AVE = 2;
	private int mStyle = STYLE_COMMON;


	public StepView(Context context) {
		this(context, null);
	}

	public StepView(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public StepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initAttrs(context, attrs);
		initData();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mWidth = w;
		mHeight = h;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		switch (mOritation) {
			case HORIZONTAL:
				drawHorizontal(canvas);
				break;
			case VERTICAL:
				drawVertical(canvas);
				break;
			default:
				break;
		}

		canvas.restore();
	}

	private void drawVertical(Canvas canvas) {
		canvas.translate(mWidth / 2, 0);
		// 每条线段的高度
		mSectionHeight = (mHeight - 2 * mPaddingTop) / (mTotalStep - 1);
		for (int i = 0; i < mTotalStep - 1; i++) {
			if (i <= mCurrentStep - 2) {
				canvas.drawLine(0, mPaddingTop + i * mSectionHeight, 0, mPaddingTop + (i + 1) * mSectionHeight, mCompletePaint);
			} else {
				canvas.drawLine(0, mPaddingTop + i * mSectionHeight, 0, mPaddingTop + (i + 1) * mSectionHeight, mUncompletePaint);
			}
		}

		for (int i = 0; i < mTotalStep; i++) {
			float cy = mPaddingTop + i * mSectionHeight;
			if (i < mCurrentStep) {
				canvas.drawCircle(0, cy, mUncompleteRadius, mCompletePaint);
			} else {
				canvas.drawCircle(0, cy, mUncompleteRadius, mUncompletePaint);
			}
		}

		for (int i = 0; i < mTotalStep; i++) {
			float cy = mPaddingTop + i * mSectionHeight;
			canvas.drawCircle(0, cy, mUncompleteRadius - mBorder, mTransparentPaint);
		}
	}

	private void drawHorizontal(Canvas canvas) {
		canvas.translate(0, mHeight / 2);
		//	每条线段的线宽
		mLineWidth = (mWidth - 2 * mPaddingLeft) / (mTotalStep - 1);

		for (int i = 0; i < mTotalStep - 1; i++) {
			if (i <= mCurrentStep - 2) {
				canvas.drawLine(mPaddingLeft + i * mLineWidth, 0, mPaddingLeft + (i + 1) * mLineWidth, 0, mCompletePaint);
			} else {
				canvas.drawLine(mPaddingLeft + i * mLineWidth, 0, mPaddingLeft + (i + 1) * mLineWidth, 0, mUncompletePaint);
			}
		}

		for (int i = 0; i < mTotalStep; i++) {
			String text = String.valueOf(i + 1);
			float textWidth = mTextPaint.measureText(text);
			float textHeight = mTextPaint.descent() + mTextPaint.ascent();

			if (i < mCurrentStep - 1) {
				canvas.drawCircle(mPaddingLeft + i * mLineWidth, 0, mUncompleteRadius, mCompletePaint);
				if (mStyle == STYLE_AVE) {
					canvas.drawText(text, mPaddingLeft + i * mLineWidth - textWidth / 2, 0 - textHeight / 2, mTextPaint);
				}
			} else if (i == mCurrentStep - 1) {
				if (mStyle == STYLE_RED) {
					mCompletePaint.setColor(mCurrentPositionCircleColor);
				}
				if (mStyle == STYLE_AVE) {
					canvas.drawCircle(mPaddingLeft + i * mLineWidth, 0, mUncompleteRadius, mCompletePaint);
				} else {
					canvas.drawCircle(mPaddingLeft + i * mLineWidth, 0, mCompleteRadius, mCompletePaint);
				}
				mCompletePaint.setColor(mCompleteColor);

				canvas.drawText(text, mPaddingLeft + i * mLineWidth - textWidth / 2, 0 - textHeight / 2, mTextPaint);
			} else {
				canvas.drawCircle(mPaddingLeft + i * mLineWidth, 0, mUncompleteRadius, mUncompletePaint);
				if (mStyle == STYLE_AVE) {
					canvas.drawText(text, mPaddingLeft + i * mLineWidth - textWidth / 2, 0 - textHeight / 2, mTextPaint);
				}
			}
		}
	}

	private void initData() {
		mCompletePaint = createPaint(mCompleteColor, Paint.Style.FILL, mLineHeight);
		mUncompletePaint = createPaint(mUncompleteColor, Paint.Style.FILL, mLineHeight);
		mTextPaint = createTextPaint(mTextColor, sp2px(10));
		mTransparentPaint = createPaint(mTextColor, Paint.Style.FILL, mLineHeight);
	}

	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StepView);
		mOritation = a.getInt(R.styleable.StepView_oritation, HORIZONTAL);
		mLineHeight = a.getDimensionPixelSize(R.styleable.StepView_lineHeight, mLineHeight);
		mStyle = a.getInt(R.styleable.StepView_step_style, STYLE_COMMON);
		mTotalStep = a.getInt(R.styleable.StepView_totalStep, 3);
		mCurrentStep = a.getInt(R.styleable.StepView_currentStep, 1);
		mUncompleteRadius = a.getDimensionPixelSize(R.styleable.StepView_uncomplete_radius, dp2px(8));
		mUncompleteColor = a.getColor(R.styleable.StepView_uncomplete_color, mUncompleteColor);
		a.recycle();
	}

	private Paint createPaint(int paintColor, Paint.Style style, int width) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setStyle(style);
		paint.setStrokeWidth(width);
		return paint;
	}

	private Paint createTextPaint(int paintColor, int textSize) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		paint.setColor(paintColor);
		paint.setTextSize(textSize);
		return paint;
	}

	private int px2dp(float paValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (paValue / scale + 0.5f);
	}

	private int dp2px(float dpValue) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	private int px2sp(float pxValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / frontScale + 0.5f);
	}

	private int sp2px(float spValue) {
		final float frontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * frontScale + 0.5f);
	}
	public void setmCurrentStep(int currentStep){
		this.mCurrentStep = currentStep;
		postInvalidate();
	}
}
